package zlx.springboot.jsp.controller;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import zlx.springboot.jsp.Service.CustomerService;
import zlx.springboot.jsp.domain.Customer;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
public class CustomerController {
    @Resource
    private CustomerService customerService;

    //获取客户的数据列表


    @GetMapping(value = "/customer/customerList")
    public String toCustomerList(Model model,String name) throws UnsupportedEncodingException {
        //解决get请求乱码问题,Tomcat低版本的的乱码问题，比如tomcat7
        /*name=new String(name.getBytes("ISO-8859-1"),"utf-8");*/
        //搜索的功能实现
        List<Customer> customerList = customerService.queryAllCustomerInfo(name);

        model.addAttribute("custmer",customerList);
        return "customer";
    }
    //跳转到修改客户页面
    @GetMapping(value = "/customer/toEdit")
    public String toEdit(Long id,Model model){
        //根据客户id查客户信息
        Customer customer = customerService.queryCustomerById(id);
        model.addAttribute("customer",customer);
        return "cache";
    }
    //修改客户信息
    @PostMapping(value = "/customer/editCustomer")
    public String editCustomer(Customer customer){
        //获取修改客户信息对象
        List<Customer> info = customerService.updateCustomerInfoByID(customer);
        System.out.println(info);
        return "redirect:/customer/customerList";
    }
    //根据id删除客户
    @GetMapping(value = "/customer/deleteCustomer")
    public String deleteCustomer(Long id){
        //获取修改客户信息对象
       customerService.deleteCustomerInfoByID(id);
        return "redirect:/customer/customerList";
    }
}
